#include "StdAfx.h"
#include "RunRecord.h"
#include "common_func.h"

CRunRecord::CRunRecord(void)
{
}

BOOL CRunRecord::Init(const TCHAR *db_path)
{
	TCHAR connectionString[1024] = {0};
	BOOL bret = FALSE;

	CoInitialize(NULL);
	m_pConnection.CreateInstance(__uuidof(Connection));
	try {   
		_tcscpy(connectionString, TEXT("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="));
		_tcscat(connectionString, db_path);
		_tcscat(connectionString, TEXT(";Persist Security Info=FALSE;Jet OLEDB:Database Password=calos"));
		m_pConnection->Open(connectionString,TEXT("admin"),TEXT(""),adModeUnknown);
		bret = TRUE;
	} catch(_com_error e) {

	} 
	return bret;
}

_RecordsetPtr CRunRecord::GetAllData()
{
	COleVariant effectRows((long)0);
	try {
		m_pRecordset = m_pConnection->Execute(TEXT("select * from run"), &effectRows, adCmdText);
		COleVariant val;
		while(!m_pRecordset->adoEOF){
			val = m_pRecordset->GetCollect(_variant_t(TEXT("start_time")));
			val = m_pRecordset->GetCollect(_variant_t((long)1));
			m_pRecordset->MoveNext();
		}
	} catch(_com_error e){
		return NULL;
	}
	return m_pRecordset;
}

BOOL CRunRecord::CheckDataBase()
{
	BOOL bret = FALSE;

	return bret;
}

CRunRecord::~CRunRecord(void)
{
	CoUninitialize();
}

BOOL CRunRecord::AddStartTime()
{
	BOOL bret = FALSE;
	COleDateTime opTime;
	SYSTEMTIME stime;

	if(m_pConnection.GetInterfacePtr() == NULL){
		return bret;
	}
	GetLocalTime(&stime);
	COleVariant effectRows((long)0);
	opTime.SetDateTime(stime.wYear, stime.wMonth, stime.wDay, stime.wHour, stime.wMinute, stime.wSecond);
	m_startime = opTime;
	CString opTimeStr = opTime.Format(_T("#%Y-%m-%d %H:%M:%S#"));
	CString queryString = TEXT("select * from run where start_time >") + opTimeStr;
	_RecordsetPtr tRecPtr;
	tRecPtr.CreateInstance(__uuidof(Recordset));
	try{
		tRecPtr->Open(queryString.GetString(), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
		tRecPtr->AddNew();
		tRecPtr->put_Collect(_variant_t((long)0), COleVariant(opTime));
		tRecPtr->Update();
		bret = TRUE;
	}catch(_com_error e) {
		
	}
	tRecPtr->Close();
	//tRecPtr->Release();
	return bret;
}

BOOL CRunRecord::AddEndTime()
{
	BOOL bret = FALSE;
	COleDateTime opTime;
	SYSTEMTIME stime;

	if(m_pConnection.GetInterfacePtr() == NULL){
		return bret;
	}
	GetLocalTime(&stime);
	COleVariant effectRows((long)0);
	opTime.SetDateTime(stime.wYear, stime.wMonth, stime.wDay, stime.wHour, stime.wMinute, stime.wSecond);
	CString stTimeStr = m_startime.Format(_T("#%Y-%m-%d %H:%M:%S#"));
	CString queryString = TEXT("select * from run where start_time =") + stTimeStr;
	_RecordsetPtr tRecPtr;
	tRecPtr.CreateInstance(__uuidof(Recordset));
	try{
		tRecPtr->Open(queryString.GetString(), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
		tRecPtr->MoveFirst();
		tRecPtr->put_Collect(_variant_t((long)1), COleVariant(opTime));
		tRecPtr->Update();
		bret = TRUE;
	}catch(_com_error e) {

	}
	tRecPtr->Close();
	//tRecPtr->Release();
	return bret;
}

_RecordsetPtr CRunRecord::QueryRecord(COleDateTime stTime, COleDateTime endTime)
{
	COleVariant effectRows((long)0);
	CString stTimeStr = stTime.Format(_T("#%Y-%m-%d %H:%M:%S#"));
	CString endTimeStr = endTime.Format(_T("#%Y-%m-%d %H:%M:%S#"));
	CString queryString = TEXT("select start_time, end_time, datediff('s', start_time, end_time) as diff from run where start_time in \
							   (select start_time from run where end_time <= ") + endTimeStr + TEXT("or end_time is null) and start_time >=")
							   + stTimeStr;
	if(!m_pRecordset.GetInterfacePtr()){
		m_pRecordset.CreateInstance(__uuidof(Recordset));
	} else {
		try{
			m_pRecordset->Close();
		} catch (_com_error e){

		}
	}

	try{
		m_pRecordset->Open(queryString.GetString(), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
		m_pRecordset->MoveFirst();
	}catch(_com_error e) {
		return NULL;
	}
	return m_pRecordset;
}

_RecordsetPtr CRunRecord::QueryRecord(SYSTEMTIME startime, SYSTEMTIME endtime)
{
	COleDateTime stTime,endTime;
	COleVariant effectRows((long)0);
	stTime.SetDateTime(startime.wYear, startime.wMonth, startime.wDay, startime.wHour, startime.wMinute, startime.wSecond);
	endTime.SetDateTime(endtime.wYear, endtime.wMonth, endtime.wDay, endtime.wHour, endtime.wMinute, endtime.wSecond);
	CString stTimeStr = stTime.Format(_T("#%Y-%m-%d %H:%M:%S#"));
	CString endTimeStr = endTime.Format(_T("#%Y-%m-%d %H:%M:%S#"));
	CString queryString = TEXT("select start_time, end_time, datediff('s', start_time, end_time) as diff from run where start_time in \
							   (select start_time from run where end_time <= ") + endTimeStr + TEXT("or end_time is null) and start_time >=")
								+ stTimeStr;
	if(!m_pRecordset.GetInterfacePtr()){
		m_pRecordset.CreateInstance(__uuidof(Recordset));
	} else {
		try{
			m_pRecordset->Close();
		} catch (_com_error e){
		
		}
	}
	
	try{
		m_pRecordset->Open(queryString.GetString(), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
		m_pRecordset->MoveFirst();
	}catch(_com_error e) {
		return NULL;
	}
	return m_pRecordset;
}